package pogvue.gui;

import pogvue.analysis.AAFrequency;
import pogvue.analysis.MatrixUtil;
import pogvue.io.FastaFile;
import pogvue.datamodel.*;

import java.awt.*;
import java.io.*;
import java.util.Vector;

public class ScanAlign {

    public static void main(String[] args) {
	try {
	    SequenceI[] s;

			FastaFile ff = new FastaFile(args[0],"File");
			s = ff.getSeqsAsArray();
	    
	    SequenceI[] kmer;


		  FastaFile kf = new FastaFile(args[1],"File");
			kmer = kf.getSeqsAsArray();
	    
	    AlignmentI align = new Alignment(kmer);
	    AlignmentI da    = new Alignment(s);

	    SequenceI dogseq   = null;
	    SequenceI mouseseq = null;
	    
	    if (s != null) {
		//		AlignFrame af = new AlignFrame(null,new Alignment(s));
		AlignViewport av = new AlignViewport(da,false,true,true,false);

				for (SequenceI value : s) {
					String name = value.getDisplayId();

					if (name.indexOf("mm5") >= 0) {
						mouseseq = value;
					} else if (name.indexOf("canFam1") >= 0) {
						dogseq = value;
					}
					if (value.getDisplayId().indexOf("Ensembl") >= 0) {
						av.getSelection().addElement(value);
					} else if (name.indexOf("hg17") == 0 ||
							name.indexOf("mm5") == 0 ||
							name.indexOf("rn3") == 0 ||
							name.indexOf("canFam1") == 0) {
						SequenceI cg = new Sequence("CpG_" + value.getDisplayId(), value.getSequence());
						av.getAlignment().addSequence(cg);
						SequenceI gc = new Sequence("GC_" + value.getDisplayId(), value.getSequence());
						av.getAlignment().addSequence(gc);
						if (name.indexOf("hg17") < 0) {
							SequenceI ll = new Sequence("LL_" + value.getDisplayId(), value.getSequence());
							av.getAlignment().addSequence(ll);
						}
					}
				}

				File file1 = new File(args[2]);
		
		BufferedInputStream bis1      = new BufferedInputStream(new FileInputStream(file1));
		DataInputStream     dataIn1   = new DataInputStream(bis1);
		
		double [][] matrixMethDog     = MatrixUtil.readMatrix(dataIn1);
		double [][] matrixUnmethDog   = MatrixUtil.readMatrix(dataIn1);
		
		double [][] matrixMethMouse   = MatrixUtil.readMatrix(dataIn1);
		double [][] matrixUnmethMouse = MatrixUtil.readMatrix(dataIn1);
		
		double [][] matrixMethRat     = MatrixUtil.readMatrix(dataIn1);
		double [][] matrixUnmethRat   = MatrixUtil.readMatrix(dataIn1);
		
		double[] pi2 = MatrixUtil.get_pi(41.0,true);
		double[] pi1 = MatrixUtil.get_pi(41.0,false);
		
		av.setMethDogMatrix(matrixMethDog);
		av.setUnmethDogMatrix(matrixUnmethDog);
		
		av.setMethMouseMatrix(matrixMethMouse);
		av.setUnmethMouseMatrix(matrixUnmethMouse);
		
		av.setMethRatMatrix(matrixMethRat);
		av.setUnmethRatMatrix(matrixUnmethRat);
		
		av.setMethPi(pi2);
		av.setUnmethPi(pi1);
		
		av.setRenderer(new KmerRenderer());
		av.setFont(new Font("Courier",Font.PLAIN,0));
		av.setCharWidth(0.1,"ScanAlign");
		av.setKmers(align.getSequences());
		
		// Find all human CpG peaks
		SequenceI seq0 = s[0];
		
		int window = 500;
		int step   = 100;
		
		int start = window/2;
		int end   = seq0.getLength()-window/2;
		
		Vector hpeaks    = AAFrequency.findKmerPeaks(seq0,seq0,    start,end,window,step,av.getKmers());
		Vector mpeaks    = AAFrequency.findKmerPeaks(seq0,mouseseq,start,end,window,step,av.getKmers());
		Vector dpeaks    = AAFrequency.findKmerPeaks(seq0,dogseq,  start,end,window,step,av.getKmers());
		
	
		int j = 0;

		String[] bases = {"A","C","G","T"};

		while (j < hpeaks.size()) { 
		    
		    SequenceFeature sf = (SequenceFeature)hpeaks.elementAt(j);

		    int cpgstart = sf.getStart()-250;
		    int cpgend   = sf.getEnd() + 250;

		    String str1 = seq0.getSequence().substring(cpgstart,cpgend);
		    String strd = dogseq.getSequence().substring(cpgstart,cpgend);
		    String strm = mouseseq.getSequence().substring(cpgstart,cpgend);

		    int k = 0;

		    int hcount = AAFrequency.countMotifs(str1,"CG");

		    int dcount = AAFrequency.countMotifs(strd,"CG");
		    int mcount = AAFrequency.countMotifs(strm,"CG");

		    int mcons  = AAFrequency.countConservedMotifs(str1,strm,"CG");
		    int dcons  = AAFrequency.countConservedMotifs(str1,strd,"CG");

		    int mbases = AAFrequency.countBases(strm,bases);
		    int dbases = AAFrequency.countBases(strd,bases);

		    System.out.println("Peak " + 
				       args[0] + "\t" + 
				       cpgstart + "\t" + 
				       cpgend + "\t" + 
				       hcount + "\t" + 
				       mcount + "\t" + 
				       mcons + "\t" + 
				       mbases + "\t" + 
				       dcount + "\t" + 
				       dcons + "\t" + 
				       dbases);
	   		    
		    j++;
		}
		
		System.exit(0);

		/* For each peak - calculate the following
		 *
		 * Mouse sequence
		 * Mouse CpG peak
		 * Mouse conserved CpGs
		 * Mouse log likelihood
		 * Mouse pid
		 *
		 * Dog sequence
		 * Dog CpG peak
		 * Dog conserved CpGs
		 * Dog log likelihood
		 * Dog pid
		 *
		 * Rat?
		 * 
		 * distance to nearest gene
		 * distance to nearest exon
		 * 
		 * overlap with repeats
		 * repeat density
		 * types of repeats within +- 1kb
		 *
		 */

	    //af.setVisible(true);

	    }
	} catch (IOException e) {
	    System.out.println("Exception " + e);
	}
    }
    public int seqOverlap(SequenceI seq, SequenceFeature sf) {
	    
	int start = sf.getStart();
	int end   = sf.getEnd();
	
	String str = seq.getSequence().substring(start-1,end);
	
	str = str.toUpperCase();
	
	char[] str_arr = str.toCharArray();
	
	int i = 0;
	
	int count = 0;
	
	while (i < str_arr.length) {
	    if (str_arr[i] == 'A' ||
		str_arr[i] == 'C' ||
		str_arr[i] == 'G' ||
		str_arr[i] == 'T') {
		count++;
	    }
	    i++;
	}
	
	return count;
    }

}

